﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace MonoFrameWork
{

    public class Animation
    {
        // Khai báo các biến của lớp
        Texture2D spriteStrip;// Đối tượng chứa hình ảnh cần hoạt động

        // cho phép lặp hay không việc vẽ đối tượng
        public AnimationStatus active;

        // Thông tin của iframe
        public int fWidth;
        public int fHeight;
        public int fCount;
        public int currentFrame;
        public int frameTime;
        public int colum;
        //Các khung vuông bao trùm
        Rectangle sourceRect=new Rectangle();
        Rectangle destinationRect = new Rectangle();

        // Định nghĩa các vector tọa độ và hình ảnh
        public Vector2 position;
        public Vector2 velocity;
        Color color;

        // Thông tin thêm
        public int elapsedTime = 0;// Thời gian qua tính từ lúc bắt đầu
        public bool looping;

        public Vector2 Position
        {
            get { return this.position; }
            set { this.position = value; }
        }
        public Vector2 Velocity
        {
            get { return this.velocity; }
            set { this.velocity = value; }
        }
        public int Colum
        {
            get { return colum; }
            set { this.colum = value; }
        }
        public AnimationStatus Active
        {
            get { return active; }
            set { active = value; }
        }
        public void Initialize(Texture2D texture, Vector2 position, int fWidth, int fHeight, int fCount, int fTimer,int colum, bool looping)
        {
            this.spriteStrip = texture;
            this.position = position;
            this.fWidth = fWidth;
            this.fHeight = fHeight;
            this.fCount = fCount;
            this.frameTime = fTimer;
            this.color = Color.White;
            this.looping = looping;
            this.colum = colum;
            active = AnimationStatus.Active;
            sourceRect = new Rectangle(currentFrame * fWidth, 0, fWidth, fHeight);

            destinationRect = new Rectangle((int)position.X, (int)position.Y, fWidth, fHeight);
        }
        public void Initialize(Texture2D texture, Vector2 position, int fWidth, int fHeight, int fCount, int fTimer, bool looping)
        {
            this.spriteStrip = texture;
            this.position = position;
            this.fWidth = fWidth;
            this.fHeight = fHeight;
            this.fCount = fCount;
            this.frameTime = fTimer;
            this.color = Color.White;
            this.looping = looping;
            this.colum = texture.Width / fWidth;
            active = AnimationStatus.Active;
            sourceRect = new Rectangle(currentFrame * fWidth, 0, fWidth, fHeight);

            destinationRect = new Rectangle((int)position.X, (int)position.Y, fWidth, fHeight);
        }
        public void Initialize(Texture2D texture, Vector2 position, int fWidth, int fHeight, int fCount, int fTimer, Color color, bool looping)
        {
            this.spriteStrip = texture;
            this.position = position;
            this.fWidth = fWidth;
            this.fHeight = fHeight;
            this.fCount = fCount;
            this.frameTime = fTimer;
            this.color = color;
            this.looping = looping;
            this.colum = texture.Width / fWidth;
            active = AnimationStatus.Active;
            sourceRect = new Rectangle(currentFrame * fWidth, 0, fWidth, fHeight);

            destinationRect = new Rectangle((int)position.X, (int)position.Y, fWidth, fHeight);
        }
        public void Initialize(Texture2D texture, Vector2 position, int fWidth, int fHeight, int fCount, int fTimer,int colum, Color color, bool looping)
        {
            this.spriteStrip = texture;
            this.position = position;
            this.fWidth = fWidth;
            this.fHeight = fHeight;
            this.fCount = fCount;
            this.frameTime = fTimer;
            this.color = color;
            this.looping = looping;
            this.colum = colum;
            active = AnimationStatus.Active;
            sourceRect = new Rectangle(currentFrame * fWidth, 0, fWidth, fHeight);

            destinationRect = new Rectangle((int)position.X, (int)position.Y, fWidth, fHeight);
        }

        public void Update(GameTime gameTime)
        {
            if (active == AnimationStatus.NoActive)
                return;
            position += velocity;
            elapsedTime += (int)gameTime.ElapsedGameTime.TotalMilliseconds;
            if (elapsedTime > frameTime)
            {
                currentFrame++;
                if (currentFrame == fCount)
                {
                    currentFrame = 0;
                    if (looping == false)
                        active = AnimationStatus.NoActive;
                }
                elapsedTime = 0;
            }
            int selectedRow = currentFrame / colum;
            int selectedColum = currentFrame % colum;
            sourceRect = new Rectangle(selectedColum * fWidth, selectedRow * fHeight, fWidth, fHeight);
            destinationRect = new Rectangle((int)position.X, (int)position.Y, fWidth, fHeight);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            if (active==AnimationStatus.Active)
            {
                spriteBatch.Draw(spriteStrip, destinationRect, sourceRect, color);
            }
        }
        public Rectangle getBound()
        {
            return new Rectangle((int)position.X, (int)position.Y, fWidth, fHeight);
        }
        public bool CheckCollides(Sprite sprite)
        {
            return this.getBound().Intersects(sprite.getBound());
        }
        public bool CheckCollides(SpriteAnimation sprite)
        {
            return this.getBound().Intersects(sprite.getBound());
        }
        public bool CheckCollides(Animation animation)
        {
            return this.getBound().Intersects(animation.getBound());
        }
        public bool CheckCollides(Texture2D texture, Vector2 position)
        {
            Rectangle rect = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
            return this.getBound().Intersects(rect);
        }
    }
}
